package com.shao.demo.feignconsumer.common.wapper;

import com.shao.demo.feignconsumer.common.constans.StatusCode;

/**
 * @author zhiqi.shao
 * @Date 2018/5/23 16:19
 */
public class ResponseEntityWrapper<T> {
    private int statusCode;
    private String statusMessage;
    private Object data;

    public ResponseEntityWrapper() {
    }

    private ResponseEntityWrapper(ResponseEntityWrapper.Builder builder) {
        this.statusCode = builder.c;
        this.statusMessage = builder.m;
        this.data = builder.o;
    }

    public static <T> ResponseEntityWrapper success(T data) {
        return (new ResponseEntityWrapper.Builder()).statusCode(StatusCode.SUCCESS.getStatusCode()).statusMessage(StatusCode.SUCCESS.getStatusMessage()).data(data).build();
    }

    public static ResponseEntityWrapper error(int code, String message) {
        return (new ResponseEntityWrapper.Builder()).statusCode(code).statusMessage(message).build();
    }

    public static ResponseEntityWrapper error(StatusCode statusCode) {
        return error(statusCode.getStatusCode(), statusCode.getStatusMessage());
    }

    public int getStatusCode() {
        return this.statusCode;
    }

    public String getStatusMessage() {
        return this.statusMessage;
    }

    public Object getData() {
        return this.data;
    }

    public static class Builder {
        private int c;
        private String m;
        private Object o;

        public Builder() {
        }

        public ResponseEntityWrapper.Builder statusCode(int code) {
            this.c = code;
            return this;
        }

        public ResponseEntityWrapper.Builder statusMessage(String message) {
            this.m = message;
            return this;
        }

        public <T> ResponseEntityWrapper.Builder data(T data) {
            this.o = data;
            return this;
        }

        public <T> ResponseEntityWrapper.Builder with(StatusCode statusCode, T data) {
            return this.statusMessage(statusCode.getStatusMessage()).statusCode(statusCode.getStatusCode()).data(data);
        }

        public ResponseEntityWrapper build() {
            return new ResponseEntityWrapper(this);
        }
    }
}
